package script.reciteWord
{
	import eventUtil.EventCenter;
	
	import laya.components.Script;
	import laya.debug.tools.comps.Rect;
	import laya.display.Input;
	import laya.events.Event;
	import laya.ui.TextInput;
	import laya.utils.Handler;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.reciteWord.ReciteDateVo;
	import model.reciteWord.ReciteWordVo;
	
	import script.ViewManager;
	import script.reciteWord.items.ReciteWordCell;
	
	import ui.learn.EnglishWordAddPanelUI;
	
	public class EnglishWordAddController extends Script
	{
		private var uiSkin:EnglishWordAddPanelUI;
		private var curReciteDateVo:ReciteDateVo;
		private var param;

		private var curDeleteWord:ReciteWordVo;
		public function EnglishWordAddController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as EnglishWordAddPanelUI;
			
			uiSkin.wordList.itemRender = ReciteWordCell;
			curReciteDateVo = param as ReciteDateVo;
			
			//uiSkin.orderList.vScrollBarSkin = "";
			uiSkin.wordList.repeatX = 1;
			uiSkin.wordList.spaceY = 10;
			
			uiSkin.wordList.renderHandler = new Handler(this, updateWordlist);
			uiSkin.wordList.selectHandler = new Handler(this,selectWord);
			uiSkin.wordList.selectEnable = true;
			uiSkin.wordList.array = curReciteDateVo.words;;
			
			uiSkin.addPanel.visible = false;
			uiSkin.addwordBtn.on(Event.CLICK,this,function(){
				
				uiSkin.addPanel.visible = true;
			})
				
			uiSkin.closeAdd.on(Event.CLICK,this,function(){
				
				uiSkin.addPanel.visible = false;
			})
				
				
			uiSkin.emglishWord.maxChars = 20;
			uiSkin.emglishWord.restrict = "a-z" + "A-Z";
			uiSkin.chineseWord.maxChars = 20;
			uiSkin.sentenceInput.maxChars = 100;
			
			uiSkin.confirmAdd.on(Event.CLICK,this,onConfirmAdd);
			EventCenter.instance.on(EventCenter.DELETE_RECITE_WORD,this,ondeleteWord);

			//var postdata:String = "classlevelId=" + param;
			//HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getLessons,this,onGetLessonInfo,postdata,"post");
			
		}
		
		private function onConfirmAdd():void
		{
			if(uiSkin.emglishWord.text.length == 0)
			{
				ViewManager.showAlert("请输入英文单词");
				return;
			}
			if(uiSkin.chineseWord.text.length == 0)
			{
				ViewManager.showAlert("请输入中文");
				return;
			}
			
			var postdata:String = "createtime=" + curReciteDateVo.createTime + "&belongLevel=" + curReciteDateVo.belongLevel + "&english=" + uiSkin.emglishWord.text + "&chinese=" + uiSkin.chineseWord.text + "&sentence=" + uiSkin.sentenceInput.text;
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.addReciteWords,this,onAddBack,postdata,"post");
		}
		
		private function onAddBack(data:Object):void
		{
			if(this.destroyed)
				return;
			
			
			var result:Object = JSON.parse(data);
			if(result.code == HttpErrorCode.OK)
			{
				var recitevo:ReciteWordVo = new ReciteWordVo();
				recitevo.chinese = uiSkin.chineseWord.text;
				recitevo.word = uiSkin.emglishWord.text;
				recitevo.sentence = uiSkin.sentenceInput.text;
				this.curReciteDateVo.words.push(recitevo);
				uiSkin.wordList.array = curReciteDateVo.words;
				
				uiSkin.addPanel.visible = false;

			}
		}
		private function updateWordlist(cell:ReciteWordCell):void
		{
			cell.setData(cell.dataSource);
		}
		
		public function selectWord(index:int):void
		{
			if(index < 0)
				return;
			
			
		}
		
		private function ondeleteWord(word:ReciteWordVo):void
		{
			curDeleteWord = word;
			
			var postdata:String = "id=" +curReciteDateVo.id + "&englishword=" + word.word;
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.deleteReciteWord,this,englishword,postdata,"post");

		}
		
		private function englishword(data:*):void
		{
			var result:Object = JSON.parse(data);
			if(result.code == HttpErrorCode.OK)
			{
				for(var i:int=0;i < curReciteDateVo.words.length;i++)
				{
					if(curReciteDateVo.words[i].word == curDeleteWord.word)
					{
						curReciteDateVo.words.splice(i,1);
						break;
					}
				}
				
				this.uiSkin.wordList.array  =curReciteDateVo.words;
			}
		}
		
		
		public override function onDestroy():void
		{
			EventCenter.instance.off(EventCenter.DELETE_RECITE_WORD,this,ondeleteWord);

		}
	}
}